package ShapeService;

import ShapeService.bounceboxframework.Shape;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
import ShapeService.bouncebox.*;
import ShapeService.bouncebox.Rectangle;

import java.awt.*;

public class ShapeService {
    public static ArrayList<Shape> parseFile(String filePath) throws IOException {
        FileInputStream fileIn = new FileInputStream(filePath);
        Scanner scan = new Scanner(fileIn);
        ArrayList<Circle> circles = new ArrayList<>();
        ArrayList<Square> squares = new ArrayList<>();
        ArrayList<Triangle> triangles = new ArrayList<>();
        ArrayList<Rectangle> rectangles = new ArrayList<>();

        while (scan.hasNext()){
            String type = scan.next();

            if (type.equalsIgnoreCase("Circle")) {
                int x = scan.nextInt();
                int y = scan.nextInt();
                int radius = scan.nextInt();

                //check if there is a velocity for the circle
                if(scan.hasNextInt())
                {
                    int vx = scan.nextInt();
                    int vy = scan.nextInt();
                    Circle c1 = new Circle(x, y, radius);
                    //c1.setColor(Color.BLACK);

                    //check if there is a color for the circle
                    if(scan.hasNextInt())
                    {
                        int r = scan.nextInt();
                        int g = scan.nextInt();
                        int b = scan.nextInt();
                        c1.setColor(new Color(r, g, b));
                    }
                    else
                    {
                        c1.setColor(Color.BLACK);
                    }
                    c1.setVelocity(vx, vy);
                    circles.add(c1);
                }
                else
                {
                    Circle c1 = new Circle(x, y, radius);
                    //c1.setColor(Color.BLACK);
                    //check if there is a color for the circle
                    if(scan.hasNextInt())
                    {
                        int r = scan.nextInt();
                        int g = scan.nextInt();
                        int b = scan.nextInt();
                        c1.setColor(new Color(r, g, b));
                    }
                    else{
                        c1.setColor(Color.BLACK);
                    }
                    circles.add(c1);
                }
            }
            else if (type.equalsIgnoreCase("Square")) {
                int x = scan.nextInt();
                int y = scan.nextInt();
                int w = scan.nextInt();
                if(scan.hasNextInt()){
                    int vx = scan.nextInt();
                    int vy = scan.nextInt();
                    Square s1 = new Square(x, y, w);
                    //s1.setColor(Color.BLACK);
                    //check if there is a color for the square
                    if(scan.hasNextInt())
                    {
                        int r = scan.nextInt();
                        int g = scan.nextInt();
                        int b = scan.nextInt();
                        s1.setColor(new Color(r, g, b));
                    }
                    else
                    {
                        s1.setColor(Color.BLACK);
                    }
                    s1.setVelocity(vx, vy);
                    squares.add(s1);
                }
                else{
                    Square s1 = new Square(x, y, w);
                    //s1.setColor(Color.BLACK);
                    //check if there is a color for the square
                    if(scan.hasNextInt())
                    {
                        int r = scan.nextInt();
                        int g = scan.nextInt();
                        int b = scan.nextInt();
                        s1.setColor(new Color(r, g, b));
                    }
                    else{
                        s1.setColor(Color.BLACK);
                    }
                    squares.add(s1);
                }
            }
            else if (type.equalsIgnoreCase("Triangle")) {
                int x = scan.nextInt();
                int y = scan.nextInt();
                int base = scan.nextInt();
                int height = scan.nextInt();
                if (scan.hasNextInt()) {
                    int vx = scan.nextInt();
                    int vy = scan.nextInt();
                    Triangle t1 = new Triangle(x, y, base, height);
                    //t1.setColor(Color.BLACK);
                    //check if there is a color for the triangle
                    if(scan.hasNextInt())
                    {
                        int r = scan.nextInt();
                        int g = scan.nextInt();
                        int b = scan.nextInt();
                        t1.setColor(new Color(r, g, b));
                    }
                    else{
                        t1.setColor(Color.BLACK);
                    }
                    t1.setVelocity(vx, vy);
                    triangles.add(t1);
                } else {
                    Triangle t1 = new Triangle(x, y, base, height);
                    //t1.setColor(Color.BLACK);
                    //check if there is a color for the triangle
                    if(scan.hasNextInt())
                    {
                        int r = scan.nextInt();
                        int g = scan.nextInt();
                        int b = scan.nextInt();
                        t1.setColor(new Color(r, g, b));
                    }
                    else{
                        t1.setColor(Color.BLACK);
                    }
                    triangles.add(t1);
                }
            }
            else if(type.equalsIgnoreCase("Rectangle")){
                int x = scan.nextInt();
                int y = scan.nextInt();
                int width = scan.nextInt();
                int height = scan.nextInt();
                if(scan.hasNextInt()){
                    int vx = scan.nextInt();
                    int vy = scan.nextInt();
                    Rectangle r1 = new Rectangle(x, y, width, height);
                    //r1.setColor(Color.BLACK);
                    //check if there is a color for the rectangle
                    if(scan.hasNextInt())
                    {
                        int r = scan.nextInt();
                        int g = scan.nextInt();
                        int b = scan.nextInt();
                        r1.setColor(new Color(r, g, b));
                    }
                    else{
                        r1.setColor(Color.BLACK);
                    }
                    r1.setVelocity(vx, vy);
                    rectangles.add(r1);
                }
                else{
                    Rectangle r1 = new Rectangle(x, y, width, height);
                    //r1.setColor(Color.BLACK);
                    //check if there is a color for the rectangle
                    if(scan.hasNextInt())
                    {
                        int r = scan.nextInt();
                        int g = scan.nextInt();
                        int b = scan.nextInt();
                        r1.setColor(new Color(r, g, b));
                    }
                    else{
                        r1.setColor(Color.BLACK);
                    }
                    rectangles.add(r1);
                }
            }
            else{
                System.err.println(type + " is not a recognized shape");
                scan.nextLine();
            }
        }

        ArrayList<Shape> shapes = new ArrayList<>();

        shapes.addAll(circles);
        shapes.addAll(squares);
        shapes.addAll(triangles);
        shapes.addAll(rectangles);

        return shapes;
    }

    public static HashMap<String, Integer> countByType(ArrayList<Shape> shapes)
    {
        HashMap<String, Integer> count = new HashMap<>();
        for (Shape shape : shapes) {
            if (shape instanceof Circle) {
                count.put("Circle", count.getOrDefault("Circle", 0) + 1);
            }
            else if (shape instanceof Square) {
                count.put("Square", count.getOrDefault("Square", 0) + 1);
            }
            else if (shape instanceof Triangle) {
                count.put("Triangle", count.getOrDefault("Triangle", 0) + 1);
            }
            else if (shape instanceof Rectangle) {
                count.put("Rectangle", count.getOrDefault("Rectangle", 0) + 1);
            }
        }

        return count;
    }

    double totalArea(ArrayList <Shape> shapes)
    {
        double area = 0;
        for (Shape shape : shapes) {
            area += shape.getMass();
        }
        return area;
    }

    public void displayShapes(ArrayList<Shape> shapes)
    {
        BounceBox box = new BounceBox(700, 500);
        for (Shape shape : shapes) {
            box.addShape(shape);
        }
        box.start();
    }
}
